package com.rockbb.common.simpleframe;

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.common.config.SpringContext;


public class ShutdownListener implements ServletContextListener{

	private static Logger logger = Logger.getLogger(ShutdownListener.class);

	public void contextInitialized(ServletContextEvent event) {
		// Do nothing;
	}

	public void contextDestroyed(ServletContextEvent event) {
		SpringContext.getCacheManagerBean().shutdown();

		Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            try {
                DriverManager.deregisterDriver(driver);
                logger.info(String.format("deregistering jdbc driver: %s", driver));
            } catch (SQLException e) {
            	logger.error(String.format("Error deregistering driver %s", driver), e);
            }
        }
	}
}
